<script setup>
import Info from './info.vue'
import {onMounted, ref} from 'vue'
import {pageApi,deleteApi} from "../../../api/grade";
import {allApi} from "../../../api/course";
import {pageGcApi,deleteGcApi,saveGcApi,updateGcApi} from "../../../api/gradecourse";
import {ElMessage, ElMessageBox} from "element-plus";
const tableData = ref({})

// 表单状态
const filterState = ref(true)
const dates=ref([])
// 表单数据 -- 查询条件
const formData = ref({
  page: 1,
  size: 10,
  name: '',
  minSdate:null,
  maxSdate:null
})

onMounted(async ()=>{
  loadData()
})

const loadData=async ()=>{
  if(dates.length>0){
      formData.value.minSdate=dates[0]
      formData.value.maxSdate=dates[1]
  }
  const r=await pageApi(formData.value)
  tableData.value=r.data;
}
// 编辑
const authEditRef = ref()
const onAuthEdit = (row) => {
  authEditRef.value.state = true
  // 回显
  if (row) {
    authEditRef.value.formData.id = row.id
    authEditRef.value.formData.type = row.type
    authEditRef.value.formData.name = row.name 
    authEditRef.value.formData.startDate = row.startDate
    authEditRef.value.formData.endDate = row.endDate
    authEditRef.value.formData.info = row.info
    authEditRef.value.formData.askEtc = row.askEtc
    authEditRef.value.formData.askExam = row.askExam
    authEditRef.value.formData.askStudy = row.askStudy
    authEditRef.value.formData.credit = row.credit
  }
}

// 权限删除
const onDelete = (row) => {
  ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    //接口请求 删除角色
    const r=await deleteApi({id:row.id});
    if(r.code==0){
      ElMessage({
        type: 'success',
        message: '删除成功'})
        //重新加载请求
        loadData()
      }else {
        ElMessage.error(r.data)
      }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '用户取消删除'
    })
  })
}

const state=ref(false)
const gradeCourse=ref({})
const gcourses=ref([])
const courses=ref([])
const onShowItem=(row)=>{
  gradeCourse.value.gradeId=row.id
  state.value=true
  //请求接口 获取班级对应的课程列表
  loadGradeCourse()
  loadCourses()
}
const loadCourses=async ()=>{
  const r=await allApi()
  courses.value=r.data
}
const loadGradeCourse=async()=>{
  const r=await pageGcApi({gid:gradeCourse.value.gradeId})
  gcourses.value=r.data
}
const addItem=ref(false)

const onEditItem=(row)=>{
  addItem.value=true

  gradeCourse.value.id=row.id
  gradeCourse.value.courseId=row.courseId
  gradeCourse.value.type=row.type
  gradeCourse.value.info=row.ifno
  gradeCourse.value.score=row.score
  gradeCourse.value.demand=row.demand
}
const onDelItem=(row)=>{
   ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    //接口请求 删除班级的课程
    const r=await deleteGcApi({id:row.id});
    if(r.code==0){
      ElMessage.success("删除班级课程成功")
        //重新加载请求
        loadGradeCourse()
      }else {
        ElMessage.error(r.data)
      }
    })
}
const onAdd=async()=>{
  let r;
  if(gradeCourse.value.id>0){
    r=await updateGcApi(gradeCourse.value)
  }else{
    r=await saveGcApi(gradeCourse.value)
  }
  if(r.code==0){
    ElMessage.success("操作成功")
    loadGradeCourse()
    addItem.value=false
    gradeCourse.value={gradeId:gradeCourse.value.gradeId}
  }else{
    ElMessage.error(r.data)
  }
}
const dateStatus=(date1,date2)=>{
  if(date1!=undefined && date2!=undefined){
    let date=new Date().toLocaleDateString()
    let start=new Date(date1).toLocaleDateString()
    let end=new Date(date2).toLocaleDateString()
    if(date > end){
      return "<p style='color:green'>已毕业</p>"
    }else if(start>date){
      return "<p style='color:red'>未开班</p>"
    }else{
      return "<p style='color:blue'>进行中</p>"
    }
  }
}
</script>
<template>
  <Box>
    <!-- 逻辑层 -->
    <Info ref="authEditRef" @flush="loadData"/>
    <!-- 逻辑层 -->

    <!-- 筛选 -->
    <template #filter v-if="filterState">
      <el-form :inline="true" :model="formData">
        <el-form-item label="班级名称">
          <el-input placeholder="请输入班级名称" v-model="formData.name" />
        </el-form-item>
        <el-form-item label="开班日期">
          <el-date-picker
            v-model="dates"
            type="datetimerange"
            value-format="YYYY-MM-DD"
            range-separator="到"
            start-placeholder="起始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadData"> <span class="iconfont icon-sousuo"></span>搜索 </el-button>
          <el-button type="default"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- 筛选 -->

    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="onAuthEdit">
          <span class="iconfont icon-jiahao"></span>添加
        </el-button>
      </el-col>

      <el-col :span="12" style="text-align: right">
        <el-button circle @click="filterState = !filterState">
          <span class="iconfont icon-sousuo"></span>
        </el-button>
        <el-button circle>
          <span class="iconfont icon-shuaxin"></span>
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组 -->

    <el-table :data="tableData.data" :border="true" class="qfTable" row-key="id" default-expand-all>
      <el-table-column label="序号" prop="id" align="center"></el-table-column>
      <el-table-column label="名称" prop="name" align="center"></el-table-column>
      <el-table-column label="类型" prop="type" align="center"></el-table-column>
      <el-table-column label="班级状态" prop="type" align="center">
      <template #default="scoped">
          <div v-html='dateStatus(scoped.row.startDate,scoped.row.endDate)'/>
      </template>
      </el-table-column>
      <el-table-column label="开班日期" prop="startDate" align="center"></el-table-column>
      <el-table-column label="毕业日期" prop="endDate" align="center"></el-table-column>
      <el-table-column label="学分" prop="credit" align="center"></el-table-column>
      <el-table-column label="说明信息" prop="info" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="ctime" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="scoped">
          <el-button type="primary" link size="small" @click="onAuthEdit(scoped.row)">
            <span class="iconfont icon-bianji"></span>修改
          </el-button>
          <el-button type="primary" link size="small" @click="onDelete(scoped.row)">
            <span class="iconfont icon-shanchu"></span>删除
          </el-button>
          <el-button type="primary" link size="small" @click="onShowItem(scoped.row)">
            <span class="iconfont icon-shanchu"></span>查看课程列表
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        v-model:current-page="formData.page"
        v-model:page-size="formData.size"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="loadData"
        @current-change="loadData"
    />

<!--    班级课程 -->
    <el-dialog
        v-model="state"
        title="班级课程列表"
        width="900"
        :before-close="() => (state = false)"
    >
      <!-- 按钮组 -->
      <el-row>
        <el-col :span="12">
          <el-button type="primary" @click="addItem=true">
            <span class="iconfont icon-jiahao"></span>添加班级课程
          </el-button>
        </el-col>
      </el-row>
      <!-- 按钮组 -->

      <el-table :data="gcourses" :border="true" class="qfTable" row-key="id" default-expand-all>
        <el-table-column label="序号" prop="id" align="center" width="60"></el-table-column>
        <el-table-column label="课程名称" prop="courseName" align="center"></el-table-column>
        <el-table-column label="类型" prop="type" align="center" width="100"></el-table-column>
        <el-table-column label="积分" prop="score" align="center" width="100"></el-table-column>
        <el-table-column label="结业要求" prop="demand" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="ctime" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="scoped">
            <el-button type="primary" link size="small" @click="onEditItem(scoped.row)">
              <span class="iconfont icon-shanchu"></span>修改
            </el-button>
             <el-button type="primary" link size="small" @click="onDelItem(scoped.row)">
              <span class="iconfont icon-shanchu"></span>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--  新增班级课程  -->
      <el-dialog
          v-model="addItem"
          title="新增课程资源"
          width="500"
          :before-close="() => (addItem = false)"
      >
        <!-- 内容 -->
        <el-form
            size="large"
            label-width="80"
            :model="gradeCourse"
            :rules="formRules"
            ref="formRef"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="课程名称">
               <el-select placeholder="课程名称" v-model="gradeCourse.courseId">
                <el-option v-for="c in courses" :label="c.title" :value="c.id"/>
               </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
             <el-form-item label="积分">
                <el-input type="number" placeholder="请输入积分" v-model="gradeCourse.score" />
              </el-form-item>
            </el-col>
          </el-row>
         <el-form-item label="课程类型">
                <el-select placeholder="课件类型" v-model="gradeCourse.type">
                  <el-option label="必修" value="必修"/>
                  <el-option label="选修" value="选修"/>
                </el-select>
          </el-form-item>
     
          <el-form-item label="结业要求">
            <el-input  type="textarea" :rows="3" v-model="gradeCourse.demand"/>
          </el-form-item>

          <el-form-item label="">
            <el-button type="primary" @click="onAdd">确定</el-button>
            <el-button type="default" @click="addItem = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

    </el-dialog>
  </Box>
</template>

<style lang="scss" scoped></style>
